{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.3 \u7f16\u5199\u6269\u5c55\u51fd\u6570\u64cd\u4f5c\u6570\u7ec4\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u7f16\u5199\u4e00\u4e2aC\u6269\u5c55\u51fd\u6570\u6765\u64cd\u4f5c\u6570\u7ec4\uff0c\u53ef\u80fd\u662f\u88abarray\u6a21\u5757\u6216\u7c7b\u4f3cNumpy\u5e93\u6240\u521b\u5efa\u3002\n\u4e0d\u8fc7\uff0c\u4f60\u60f3\u8ba9\u4f60\u7684\u51fd\u6570\u66f4\u52a0\u901a\u7528\uff0c\u800c\u4e0d\u662f\u9488\u5bf9\u67d0\u4e2a\u7279\u5b9a\u7684\u5e93\u6240\u751f\u6210\u7684\u6570\u7ec4\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u80fd\u8ba9\u63a5\u53d7\u548c\u5904\u7406\u6570\u7ec4\u5177\u6709\u53ef\u79fb\u690d\u6027\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u5230 Buffer Protocol .\n\u4e0b\u9762\u662f\u4e00\u4e2a\u624b\u5199\u7684C\u6269\u5c55\u51fd\u6570\u4f8b\u5b50\uff0c\n\u7528\u6765\u63a5\u53d7\u6570\u7ec4\u6570\u636e\u5e76\u8c03\u7528\u672c\u7ae0\u5f00\u7bc7\u90e8\u5206\u7684 avg(double *buf, int len) \u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "/* Call double avg(double *, int) */\nstatic PyObject *py_avg(PyObject *self, PyObject *args) {\n  PyObject *bufobj;\n  Py_buffer view;\n  double result;\n  /* Get the passed Python object */\n  if (!PyArg_ParseTuple(args, \"O\", &bufobj)) {\n    return NULL;\n  }\n\n  /* Attempt to extract buffer information from it */\n\n  if (PyObject_GetBuffer(bufobj, &view,\n      PyBUF_ANY_CONTIGUOUS | PyBUF_FORMAT) == -1) {\n    return NULL;\n  }\n\n  if (view.ndim != 1) {\n    PyErr_SetString(PyExc_TypeError, \"Expected a 1-dimensional array\");\n    PyBuffer_Release(&view);\n    return NULL;\n  }\n\n  /* Check the type of items in the array */\n  if (strcmp(view.format,\"d\") != 0) {\n    PyErr_SetString(PyExc_TypeError, \"Expected an array of doubles\");\n    PyBuffer_Release(&view);\n    return NULL;\n  }\n\n  /* Pass the raw buffer and size to the C function */\n  result = avg(view.buf, view.shape[0]);\n\n  /* Indicate we're done working with the buffer */\n  PyBuffer_Release(&view);\n  return Py_BuildValue(\"d\", result);\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u6211\u4eec\u6f14\u793a\u4e0b\u8fd9\u4e2a\u6269\u5c55\u51fd\u6570\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import array\navg(array.array('d',[1,2,3]))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy\navg(numpy.array([1.0,2.0,3.0]))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "avg([1,2,3])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "avg(b'Hello')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = numpy.array([[1.,2.,3.],[4.,5.,6.]])\navg(a[:,2])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sample.avg(a)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sample.avg(a[0])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c06\u4e00\u4e2a\u6570\u7ec4\u5bf9\u8c61\u4f20\u7ed9C\u51fd\u6570\u53ef\u80fd\u662f\u4e00\u4e2a\u6269\u5c55\u51fd\u6570\u505a\u7684\u6700\u5e38\u89c1\u7684\u4e8b\u3002\n\u5f88\u591aPython\u5e94\u7528\u7a0b\u5e8f\uff0c\u4ece\u56fe\u50cf\u5904\u7406\u5230\u79d1\u5b66\u8ba1\u7b97\uff0c\u90fd\u662f\u57fa\u4e8e\u9ad8\u6027\u80fd\u7684\u6570\u7ec4\u5904\u7406\u3002\n\u901a\u8fc7\u7f16\u5199\u80fd\u63a5\u53d7\u5e76\u64cd\u4f5c\u6570\u7ec4\u7684\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199\u5f88\u597d\u7684\u517c\u5bb9\u8fd9\u4e9b\u5e94\u7528\u7a0b\u5e8f\u7684\u81ea\u5b9a\u4e49\u4ee3\u7801\uff0c\n\u800c\u4e0d\u662f\u53ea\u80fd\u517c\u5bb9\u4f60\u81ea\u5df1\u7684\u4ee3\u7801\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ee3\u7801\u7684\u5173\u952e\u70b9\u5728\u4e8e PyBuffer_GetBuffer() \u51fd\u6570\u3002\n\u7ed9\u5b9a\u4e00\u4e2a\u4efb\u610f\u7684Python\u5bf9\u8c61\uff0c\u5b83\u4f1a\u8bd5\u7740\u53bb\u83b7\u53d6\u5e95\u5c42\u5185\u5b58\u4fe1\u606f\uff0c\u5b83\u7b80\u5355\u7684\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u5e76\u8fd4\u56de-1.\n\u4f20\u7ed9 PyBuffer_GetBuffer() \u7684\u7279\u6b8a\u6807\u5fd7\u7ed9\u51fa\u4e86\u6240\u9700\u7684\u5185\u5b58\u7f13\u51b2\u7c7b\u578b\u3002\n\u4f8b\u5982\uff0cPyBUF_ANY_CONTIGUOUS \u8868\u793a\u662f\u4e00\u4e2a\u8fde\u7eed\u7684\u5185\u5b58\u533a\u57df\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u6570\u7ec4\u3001\u5b57\u8282\u5b57\u7b26\u4e32\u548c\u5176\u4ed6\u7c7b\u4f3c\u5bf9\u8c61\u800c\u8a00\uff0c\u4e00\u4e2a Py_buffer \u7ed3\u6784\u4f53\u5305\u542b\u4e86\u6240\u6709\u5e95\u5c42\u5185\u5b58\u7684\u4fe1\u606f\u3002\n\u5b83\u5305\u542b\u4e00\u4e2a\u6307\u5411\u5185\u5b58\u5730\u5740\u3001\u5927\u5c0f\u3001\u5143\u7d20\u5927\u5c0f\u3001\u683c\u5f0f\u548c\u5176\u4ed6\u7ec6\u8282\u7684\u6307\u9488\u3002\u4e0b\u9762\u662f\u8fd9\u4e2a\u7ed3\u6784\u4f53\u7684\u5b9a\u4e49\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "typedef struct bufferinfo {\n    void *buf;              /* Pointer to buffer memory */\n    PyObject *obj;          /* Python object that is the owner */\n    Py_ssize_t len;         /* Total size in bytes */\n    Py_ssize_t itemsize;    /* Size in bytes of a single item */\n    int readonly;           /* Read-only access flag */\n    int ndim;               /* Number of dimensions */\n    char *format;           /* struct code of a single item */\n    Py_ssize_t *shape;      /* Array containing dimensions */\n    Py_ssize_t *strides;    /* Array containing strides */\n    Py_ssize_t *suboffsets; /* Array containing suboffsets */\n} Py_buffer;"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u53ea\u5173\u6ce8\u63a5\u53d7\u4e00\u4e2a\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\u3002\n\u8981\u68c0\u67e5\u5143\u7d20\u662f\u5426\u662f\u4e00\u4e2a\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\uff0c\u53ea\u9700\u9a8c\u8bc1 format \u5c5e\u6027\u662f\u4e0d\u662f\u5b57\u7b26\u4e32\u201dd\u201d.\n\u8fd9\u4e2a\u4e5f\u662f struct \u6a21\u5757\u7528\u6765\u7f16\u7801\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u3002\n\u901a\u5e38\u6765\u8bb2\uff0cformat \u53ef\u4ee5\u662f\u4efb\u4f55\u517c\u5bb9 struct \u6a21\u5757\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\n\u5e76\u4e14\u5982\u679c\u6570\u7ec4\u5305\u542b\u4e86C\u7ed3\u6784\u7684\u8bdd\u5b83\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u503c\u3002\n\u4e00\u65e6\u6211\u4eec\u5df2\u7ecf\u786e\u5b9a\u4e86\u5e95\u5c42\u7684\u7f13\u5b58\u533a\u4fe1\u606f\uff0c\u90a3\u53ea\u9700\u8981\u7b80\u5355\u7684\u5c06\u5b83\u4f20\u7ed9C\u51fd\u6570\uff0c\u7136\u540e\u4f1a\u88ab\u5f53\u505a\u662f\u4e00\u4e2a\u666e\u901a\u7684C\u6570\u7ec4\u4e86\u3002\n\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u4e0d\u5fc5\u62c5\u5fc3\u662f\u600e\u6837\u7684\u6570\u7ec4\u7c7b\u578b\u6216\u8005\u5b83\u662f\u88ab\u4ec0\u4e48\u5e93\u521b\u5efa\u51fa\u6765\u7684\u3002\n\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u51fd\u6570\u80fd\u517c\u5bb9 array \u6a21\u5757\u4e5f\u80fd\u517c\u5bb9 numpy \u6a21\u5757\u4e2d\u7684\u6570\u7ec4\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd4\u56de\u6700\u7ec8\u7ed3\u679c\u4e4b\u524d\uff0c\u5e95\u5c42\u7684\u7f13\u51b2\u533a\u89c6\u56fe\u5fc5\u987b\u4f7f\u7528 PyBuffer_Release() \u91ca\u653e\u6389\u3002\n\u4e4b\u6240\u4ee5\u8981\u8fd9\u4e00\u6b65\u662f\u4e3a\u4e86\u80fd\u6b63\u786e\u7684\u7ba1\u7406\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u540c\u6837\uff0c\u672c\u8282\u4e5f\u4ec5\u4ec5\u53ea\u662f\u6f14\u793a\u4e86\u63a5\u53d7\u6570\u7ec4\u7684\u4e00\u4e2a\u5c0f\u7684\u4ee3\u7801\u7247\u6bb5\u3002\n\u5982\u679c\u4f60\u771f\u7684\u8981\u5904\u7406\u6570\u7ec4\uff0c\u4f60\u53ef\u80fd\u4f1a\u78b0\u5230\u591a\u7ef4\u6570\u636e\u3001\u5927\u6570\u636e\u3001\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u7b49\u7b49\u95ee\u9898\uff0c\n\u90a3\u4e48\u5c31\u5f97\u53bb\u5b66\u66f4\u9ad8\u7ea7\u7684\u4e1c\u897f\u4e86\u3002\u4f60\u9700\u8981\u53c2\u8003\u5b98\u65b9\u6587\u6863\u6765\u83b7\u53d6\u66f4\u591a\u8be6\u7ec6\u7684\u7ec6\u8282\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u9700\u8981\u7f16\u5199\u6d89\u53ca\u5230\u6570\u7ec4\u5904\u7406\u7684\u591a\u4e2a\u6269\u5c55\uff0c\u90a3\u4e48\u901a\u8fc7Cython\u6765\u5b9e\u73b0\u4f1a\u66f4\u5bb9\u6613\u4e0b\u3002\u53c2\u800315.11\u8282\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}